/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.bean.BeanUtil
 *  cn.hutool.core.collection.CollUtil
 *  cn.hutool.core.util.StrUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.scheduling.annotation.Async
 *  tk.mybatis.mapper.entity.Example
 */
package com.gys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SyncUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.ConstUtil;
import com.gys.common.util.PingIpUtil;
import com.gys.data.salesReceipts.GetSyncSdSaleDInData;
import com.gys.data.salesReceipts.GetSyncSdSaleHInData;
import com.gys.mapper.GaiaSdSaleDMapper;
import com.gys.mapper.GaiaSdSaleHMapper;
import com.gys.mapper.entity.GaiaSdSaleD;
import com.gys.mapper.entity.GaiaSdSaleH;
import de.felixroske.jfxsupport.FXMLController;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import tk.mybatis.mapper.entity.Example;

@FXMLController
public class OfflineController {
    private static final Logger log = LoggerFactory.getLogger(OfflineController.class);
    @Autowired
    private GaiaSdSaleHMapper sdSaleHMapper;
    @Autowired
    private GaiaSdSaleDMapper sdSaleDMapper;

    public void isConnect() {
        if (PingIpUtil.isConnect()) {
            log.info("\u5728\u7ebf");
            CacheUtil.put("networkFlag", true);
            ConstUtil.HTTP_URL = ConstUtil.HTTP_SELF_URL;
            this.uploadSaleInfo();
        } else {
            log.info("\u79bb\u7ebf");
            CacheUtil.put("networkFlag", false);
            ConstUtil.HTTP_URL = ConstUtil.HTTP_LOCAL_URL;
        }
    }

    @Async
    public void isConnectNoData() {
        if (PingIpUtil.isConnect()) {
            log.info("\u5728\u7ebf");
            CacheUtil.put("networkFlag", true);
            ConstUtil.HTTP_URL = ConstUtil.HTTP_SELF_URL;
        } else {
            log.info("\u79bb\u7ebf");
            CacheUtil.put("networkFlag", false);
            ConstUtil.HTTP_URL = ConstUtil.HTTP_LOCAL_URL;
        }
    }

    public void uploadSaleInfo() {
        String offlineUploadFlag = (String)CacheUtil.get("offlineUploadFlag");
        if (StrUtil.isNotBlank((CharSequence)offlineUploadFlag)) {
            return;
        }
        CacheUtil.put("offlineUploadFlag", "1");
        Example example = new Example(GaiaSdSaleH.class);
        example.createCriteria().andEqualTo("gsshUpload", "0");
        List<GaiaSdSaleH> list = this.sdSaleHMapper.selectByExample(example);
        if (CollUtil.isEmpty((Collection)list)) {
            log.info("\u65e0\u9500\u552e\u5355\u9700\u8981\u4e0a\u4f20");
            CacheUtil.remove("offlineUploadFlag");
            return;
        }
        for (GaiaSdSaleH sale : list) {
            GetSyncSdSaleHInData inData = new GetSyncSdSaleHInData();
            BeanUtil.copyProperties(sale, inData);
            example = new Example(GaiaSdSaleD.class);
            example.createCriteria().andEqualTo("gssdBillNo", sale.getGsshBillNo());
            List<GaiaSdSaleD> saleDetailList = this.sdSaleDMapper.selectByExample(example);
            ArrayList<GetSyncSdSaleDInData> detailList = new ArrayList<GetSyncSdSaleDInData>();
            for (GaiaSdSaleD saleD : saleDetailList) {
                GetSyncSdSaleDInData detailData = new GetSyncSdSaleDInData();
                BeanUtil.copyProperties(saleD, detailData);
                detailList.add(detailData);
            }
            inData.setDetailList(detailList);
            HttpJson rsp = SyncUtil.uploadSaleInfo(inData);
            if (rsp.isSuccess()) {
                sale.setGsshUpload("1");
                this.sdSaleHMapper.updateByPrimaryKey(sale);
                continue;
            }
            log.info("\u4e0a\u4f20\u9500\u552e\u5355\u5931\u8d25=" + JSON.toJSONString(inData));
        }
        CacheUtil.remove("offlineUploadFlag");
    }
}

